<?php

namespace App\Library\Service\Base;

use App\Library\KuaiDi;
use App\Library\Logic\AuctionOrderLogic;
use App\Library\Logic\LogisticsLogic;
use App\Library\Logic\ShopOrderExtLogic;
use App\Library\Logic\ShopOrderLogic;
use App\Library\Logic\ShopOrderReturnLogic;
use App\Library\Service\BaseService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\JsonResponse;

class BaseLogisticService extends BaseService
{
    public function __construct(LogisticsLogic $logic)
    {
        $this->logic = $logic;
    }

    /**
     * @throws GuzzleException
     */
    public static function orderLogistics(Model $model)
    {
        if ($model->status_sign) return $model;
        else {
            if(isset($model->logistics)){
                $model->object_id=$model->logistics->object_id;
                $model->express_no=$model->logistics->express_no;
                $model->type=$model->logistics->type;
            }
            $order = ShopOrderLogic::findByWhere(['orderId' => $model->object_id], null, ['order_contacts_mobile']);
            $codeInfo = KuaiDi::ExpressCodeInfo($model->express_no);//获取物流公司标识信息
            if ($codeInfo instanceof JsonResponse) return $codeInfo->original['data']['message'];

            $info = KuaiDi::newQuery($codeInfo[0]['comCode'], $model->express_no, substr($order->order_contacts_mobile, 7, 4));

            if (isset($info['returnCode'])) return $info['message'];
            $order->order_buy_status = 5;
            $order->order_sell_status = 5;
            $order->order_confirm_time = time();
            $order->save();
            $model->info = $info;
            if (in_array($info['state'], [3, 4])) {
                if (is_null(self::checkUnsignedLogistic($model->object_id, $model->type, $model->object_id))) {
                    $orderExt = ShopOrderExtLogic::findBywhere(['orderId' => $model->object_id]);
                    $orderExt->status = 4;
                    $orderExt->status_confirm = 1;
                    $orderExt->confirm_at = now()->toDateTimeString();
                    $orderExt->status_complete = 1;
                    $orderExt->complete_at = now()->toDateTimeString();
                    $orderExt->save();
                }
                $model->status_sign = 1;
                $model->express_id = $codeInfo[0]['comCode'];
                $model->save();
            }
            return $model;
        }
    }

    /**
     *
     * @param Model $model
     * @throws GuzzleException
     */
    public function orderReturnLogistics(Model $model)
    {
        if ($model->status_sign) return $model;
        else {
            $codeInfo = KuaiDi::ExpressCodeInfo($model->express_no);//获取物流公司标识信息
            if ($codeInfo instanceof JsonResponse) return $codeInfo->original['data']['message'];
            $info = KuaiDi::newQuery($codeInfo[0]['comCode'], $model->express_no);
            if (isset($info['returnCode'])) return $info['message'];
            $model->info = $info;
            if (in_array($info['state'], [3, 4])) {
                $orderReturn = ShopOrderReturnLogic::findBywhere(['orId' => $model->object_id]);
                $orderReturn->or_buy_status = 6;
                $orderReturn->or_sell_status = 6;
                $orderReturn->save();
                $model->express_id = $codeInfo[0]['comCode'];
                $model->status_sign = 1;
                $model->save();
            }
            return $model;
        }
    }

    /**
     *
     * @param Model $model
     * @return Model|JsonResponse
     * @throws GuzzleException
     *
     */
    public function auctionOrderLogistics(Model $model): Model|JsonResponse
    {
        if ($model->status_sign) return $model;
        else {
            $codeInfo = KuaiDi::ExpressCodeInfo($model->express_no);//获取物流公司标识信息
            if ($codeInfo instanceof JsonResponse) return $codeInfo->original['data']['message'];
            $info = KuaiDi::newQuery($codeInfo[0]['comCode'], $model->express_no);
            if (isset($info['returnCode'])) return $info['message'];
            $model->info = $info;
            if (in_array($info['state'], [3, 4])) {
                $auction = AuctionOrderLogic::findBywhere(['id' => $model->object_id]);
                $auction->status = 4;
                $auction->status_sign = 1;
                $auction->status_complete = 1;
                $auction->confirm_at = now();
                $auction->auto_confirm_at = now();
                $auction->save();
                $model->express_id = $codeInfo[0]['comCode'];
                $model->status_sign = 1;
                $model->save();
            }
            return $model;
        }
    }

    public static function checkUnsignedLogistic(int $objectType, int $type, int $objectId): ?Model
    {
        return LogisticsLogic::findByWhere([
            'objectType' => $objectType,
            'type' => $type,
            'objectId' => $objectId,
            'statusSign' => 0
        ]);
    }

    /**
     *
     * @param int $id
     * @param string $expressNo
     * @param int $objectType 订单类型;0:auction;1:product;'
     * @param int $type 0:订单;1:退货;2:换货;3:补货;
     * @return void
     * @throws GuzzleException
     */
    public static function delivery(int $id, string $expressNo, int $objectType = 1, int $type = 0): void
    {
        $express = identifyOddNumbers($expressNo);
        $logistics = LogisticsLogic::findByWhere([
            'objectType' => $objectType,
            'objectId' => $id,
            'expressNo' => $expressNo,
            'type' => $type
        ]);

        if (is_null($logistics)) {
            LogisticsLogic::insertForModel([
                'objectType' => $objectType,
                'objectId' => $id,
                'type' => $type,
                'expressId' => empty($express) ? '' : $express['comCode'],
                'expressNo' => $expressNo
            ]);
        }
    }
}
